﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using Gestion_Api.AccesoDatos;
using System.Data.SqlClient;

namespace Escuela.Forms
{
    public partial class PagosAlumnosForm : Form
    {
        private Instituto inst;
        private int idAlumno;
        protected int acep;

        public PagosAlumnosForm( Instituto inst, int idAlumno)
        {
            InitializeComponent();
            this.inst = inst;
            this.idAlumno = idAlumno;
        }

        private void Nuevo_Click(object sender, EventArgs e)
        {
            
                acep = 1;
                dtFechaPAgo.Enabled = false;
                enableBtn();
                enableTxt();
            
        }

        private void cargarData2()
        {
            string query = null;
            SqlDataReader Dr;
            System.Data.DataTable dt = new System.Data.DataTable();
            AccesoDB ac = new AccesoDB();
            query = "pagos_pendientes_alumno ";
            query += this.idAlumno;

            SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            dt = ac.execDT(command);

            //dt = ac.execDT(query);

            this.dataGridView1.DataSource = dt;

        }

        protected void enableTxt()
        {
            this.dtFechaVenc.Enabled = true;
            this.dtFechaPAgo.Enabled = true;
            this.txtObservacion.Enabled = true;
            this.comboEstado.Enabled = true;
            this.txtMonto.Enabled = true;
            this.txtMontoAct.Enabled = true;
        }
        protected void enableBtn()
        {
            this.Pagos.Enabled=true;
            this.Cancelar.Enabled=true;
        }



        private void PagosAlumnosForm_Load(object sender, EventArgs e)
        {
            try
            {


                this.pagos_pendientes_alumnoTableAdapter.Fill(this.escuelaDataSet2.pagos_pendientes_alumno, idAlumno);

                //this.cargarData2();
                this.enlazarDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Se ha producido un error al cargar el formulario", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void agregarCOLInt()
        {
            //this.dataGridView1.
        }

        private void Confirmar_Click(object sender, EventArgs e)
        {
            try
            {
                int i = this.dataGridView1.CurrentRow.Index;
                if (i >= 0)
                {
                    if (this.comboEstado.Text != "Pagado")
                    {
                        int idPago = Convert.ToInt32(this.txtNumero.Text);
                        double monto = Convert.ToDouble(this.txtMontoAct.Text);

                        this.inst.registrarPago(idPago, monto);

                        MessageBox.Show("Pago Registrado con exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.pagos_pendientes_alumnoTableAdapter.Fill(this.escuelaDataSet2.pagos_pendientes_alumno, idAlumno);


                    }
                    else
                    {
                        MessageBox.Show("El Pago ya fue abonado", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                else
                {
                    MessageBox.Show("Debe Elegir un pago", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Se ha producido un error registrando el pago", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           

        }

        private void enlazarDataGrid()
        {
            try
            {
                //texto
                this.txtObservacion.DataBindings.Add("Text", dataGridView1.DataSource, "observacion");
                this.txtNumero.DataBindings.Add("Text", dataGridView1.DataSource, "id");
                this.comboEstado.DataBindings.Add("Text", dataGridView1.DataSource, "pendiente");
                this.txtMonto.DataBindings.Add("Text", dataGridView1.DataSource, "monto");
                this.dtFechaPAgo.DataBindings.Add("Text", dataGridView1.DataSource, "fecha_pago");
                this.dtFechaVenc.DataBindings.Add("Text", dataGridView1.DataSource, "fecha_vencimiento");
                //si esta vencido agrego interes
                if(DateTime.Compare(DateTime.Now, this.dtFechaVenc.Value ) == 1)
                {
                    this.txtMontoAct.Text = (Convert.ToDouble(this.txtMonto.Text) * 1.15).ToString();
                }
                else
                    txtMontoAct.Text = txtMonto.Text;
                        

               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enlazando datos al DataGrid", "Error");
            }


        }

        private void Buscar_Click(object sender, EventArgs e)
        {
            try
            {
                string query = null;
            
            System.Data.DataTable dt = new System.Data.DataTable();
            AccesoDB ac = new AccesoDB();
            query = "pagos_pendientes_alumnoAll " + this.idAlumno;
            

            SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            dt = ac.execDT(command);

            //dt = ac.execDT(query);

            this.dataGridView1.DataSource = dt;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enlazando datos al DataGridView", "Error");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.pagos_pendientes_alumnoTableAdapter.Fill(this.escuelaDataSet2.pagos_pendientes_alumno, idAlumno);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error enlazando datos al DataGrid", "Error");
            }
        }

       


    }
}
